class Admin::UsersController < Admin::BaseController
  def index
  	edit
  	render :action => 'edit'
  end
  
  def edit
  	@action_title = 'Your Profile'
  	
  	@user = current_user
  	if !@user.avatar.nil?
  	  @avatar_url = url_for(:controller => '/photos', :action => 'show', :id => @user.avatar.id)
  	end
  end
  
  def update
  	if request.post?
  	  @user = current_user
  	  
  	  if @user.update_attributes(params[:user].symbolize_keys)
  	  	if params[:photo]['photo'].length <= 0
  	  	  flash[:notice] == 'Success: saved.'
	      redirect_to :action => 'edit' and return
        end
  	  else
  	  	render :action => 'edit' and return
  	  end
  	  
  	  unless params[:photo]['photo'].content_type =~ /^image/
	    flash[:error] = 'Please select an image file to upload.'
	    @action_title = 'Your Profile'
	    render :action => 'edit' and return
	  end
	
	  unless params[:photo]['photo'].size / 1024 < 32
	  	flash[:error] = 'Please upload an image whose size less than 32KB (32768 bytes).'
	  	@action_title = 'Your Profile'
	    render :action => 'edit' and return
      end
	
	  @photo = Photo.new(params[:photo])
	  @photo.user_id = @user.id
	
	  if @photo.save
	    flash[:notice] == 'Success: saved.'
	    redirect_to :action => 'edit' and return
  	  else
        flash[:error] == 'Error: There was a problem.'
        @action_title = 'Your Profile'
	    render :action => 'edit' and return
      end
    end
  end
  
  def change_password
  	@action_title = 'Your Password'
  	@user = current_user
  	
  	if request.post?
  	  if @user.update_attributes(params[:user].symbolize_keys)
  	  	flash[:notice] = 'Success: Your password was changed.'
  	  	redirect_to :action => 'change_password' and return
  	  else
  	  	render :action => 'change_password' and return
  	  end
  	end
  end
  
end
